// 我的页面逻辑
Page({
  data: {
    userInfo: {
      name: '',
      avatar: '',
      level: ''
    },
    orderCount: 0,
    couponCount: 0,
    points: 0
  },

  onLoad() {
    console.log('我的页面加载')
    this.loadUserData()
  },

  onShow() {
    // 页面显示时重新加载用户数据
    this.loadUserData()
  },

  // 加载用户数据
  loadUserData() {
    // 模拟从缓存或服务器获取用户数据
    const userInfo = wx.getStorageSync('userInfo') || {}
    const userData = {
      orderCount: userInfo.orderCount || 0,
      couponCount: userInfo.couponCount || 0,
      points: userInfo.points || 0,
      userInfo: userInfo
    }
    
    this.setData(userData)
  },

  // 登录
  onLogin() {
    wx.navigateTo({
      url: '/pages/login/login'
    })
  },

  // 退出登录
  onLogout() {
    wx.showModal({
      title: '提示',
      content: '确定要退出登录吗？',
      success: (res) => {
        if (res.confirm) {
          // 清除用户数据
          wx.removeStorageSync('userInfo')
          this.setData({
            userInfo: {},
            orderCount: 0,
            couponCount: 0,
            points: 0
          })
          wx.showToast({
            title: '已退出登录',
            icon: 'success'
          })
        }
      }
    })
  },

  // 我的订单
  onMyOrders() {
    if (!this.data.userInfo.name) {
      this.onLogin()
      return
    }
    wx.navigateTo({
      url: '/pages/orders/orders'
    })
  },

  // 我的优惠券
  onMyCoupons() {
    if (!this.data.userInfo.name) {
      this.onLogin()
      return
    }
    wx.navigateTo({
      url: '/pages/coupons/coupons'
    })
  },

  // 我的积分
  onMyPoints() {
    if (!this.data.userInfo.name) {
      this.onLogin()
      return
    }
    wx.navigateTo({
      url: '/pages/points/points'
    })
  },

  // 我的预约
  onMyAppointments() {
    if (!this.data.userInfo.name) {
      this.onLogin()
      return
    }
    wx.navigateTo({
      url: '/pages/my-appointments/my-appointments'
    })
  },

  // 会员权益
  onMemberBenefits() {
    wx.navigateTo({
      url: '/pages/member-benefits/member-benefits'
    })
  },

  // 收货地址
  onMyAddress() {
    if (!this.data.userInfo.name) {
      this.onLogin()
      return
    }
    wx.navigateTo({
      url: '/pages/address/address'
    })
  },

  // 设置
  onSettings() {
    wx.navigateTo({
      url: '/pages/settings/settings'
    })
  },

  // 客服中心
  onCustomerService() {
    wx.makePhoneCall({
      phoneNumber: '400-123-4567'
    })
  }
})